const tagsView = {
  state: {
    visitedViews: [{name: 'Index', path: '/admin/model/index', title: '首页'}],
    cachedViews: ['Index'],
    refreshViews: [],
    isRouterAlive: true   // 用于路由刷新 
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.some(v => v.path === view.path)) return
      if (state.visitedViews.some(v => v.name === view.name)) return
      state.visitedViews.push({
        name: view.name,
        path: view.path,
        title: view.meta.title || 'no-name'
      })
      if (!view.meta.noCache) {
        state.cachedViews.push(view.name)
      }
    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path && view.path !== '/admin/model/index') {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          state.visitedViews.unshift({name: 'Index', path: '/admin/model/index', title: '首页'})
          break
        } else if (view.path === '/admin/model/index') {
          state.visitedViews = [{name: 'Index', path: '/admin/model/index', title: '首页'}]
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name && view.name !== 'Index') {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews = state.cachedViews.slice(index, i + 1)
          state.cachedViews.unshift('Index')
          break
        } else if (view.name === 'Index') {
          state.cachedViews = ['Index']
          break
        } 
      }
    },
    DEL_ALL_VIEWS: (state) => {  // 首页不能关闭
      state.visitedViews = [{name: 'Index', path: '/admin/model/index', title: '首页'}]
      state.cachedViews = ['Index']
    },
    ADD_REFRESH_VIEWS: (state, view) => {
      state.refreshViews.push(view)
      state.isRouterAlive = false
    },
    ADD_REFRESH_OTHER_VIEWS: (state, view) => {
      for (const i of state.cachedViews) {
        if (i !== view) {
          state.refreshViews.push(i)
        }
      }
      state.isRouterAlive = false
    },
    ADD_REFRESH_All_VIEWS: (state) => {
      state.refreshViews = state.cachedViews
      state.isRouterAlive = false
    },
    DEL_REFRESH_VIEWS: (state) => {
      state.refreshViews = []
      state.isRouterAlive = true
    }
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews({ commit, state }) {
      return new Promise((resolve) => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    },
    addRefreshViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('ADD_REFRESH_VIEWS', view)
      })
    },
    addRefreshOtherViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('ADD_REFRESH_OTHER_VIEWS', view)
      })
    },
    addRefreshAllViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('ADD_REFRESH_All_VIEWS', view)
      })
    },
    delRefreshViews({ commit, state }) {
      return new Promise((resolve) => {
        commit('DEL_REFRESH_VIEWS')
        resolve([...state.refreshViews])
      })
    }
  }
}

export default tagsView
